package com.cn.kehong.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.*;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;
import com.cn.kehong.utils.msgSend.HttpClientUtil;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/firstPage")
public class FirstPageController {

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/getForecast")
    @ResponseBody
    public JsonResponse<JSONObject> getForecast(String cityCode) throws Exception {
        JsonResponse<JSONObject> response = new JsonResponse<JSONObject>();
        String result = HttpClientUtil.doGet("http://t.weather.itboy.net/api/weather/city/"+cityCode);
        JSONObject res  = JSON.parseObject(result);
        response.setResult(res);
        System.out.println(res);
        response.setSuccess(true);
        return  response;
    }


    @RequestMapping("/sendMessage/{frontToken}")
    @ResponseBody
    public JsonResponse<String> sendMessageToRedis(String packageRoom,String code) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        redisTemplate.opsForList().leftPush(packageRoom,code);
        response.setSuccess(true);
        return  response;
    }

    @RequestMapping("/getMessage/{frontToken}")
    @ResponseBody
    public JsonResponse<String> getMessageFromRedis(String packageRoom) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        response.setResult("");
        long videoSize = redisTemplate.opsForList().size(packageRoom);
        if(videoSize > 0){
            Object value = redisTemplate.opsForList().rightPop(packageRoom);
            response.setResult(value.toString());
        }
        response.setSuccess(true);
        return  response;
    }




}
